package dongshi.daddy.xiaohengshuke;

import org.freeswitch.esl.client.IEslEventListener;
import org.freeswitch.esl.client.inbound.Client;
import org.freeswitch.esl.client.inbound.InboundConnectionFailure;
import org.freeswitch.esl.client.transport.event.EslEvent;

/**
 * @author 菩提树下的杨过
 */
public class InboundApp {
    public static void main(String[] args) throws InterruptedException {
        Client client = new Client();
        try {
            //连接freeswitch
            String testFsServer = "10.150.27.146";
            String devFsServer = "10.150.30.44";
//            client.connect("10.150.30.44", 8031, "Hxcc1234", 10);
            client.connect(testFsServer, 8031, "Hxcc1234", 10);
            client.addEventListener(new IEslEventListener() {
                @Override
                public void eventReceived(EslEvent event) {
                    String eventName = event.getEventName();
                    System.out.println("receive event: " + eventName);
                    System.out.println("heads: " + event.getEventHeaders());
                    //这里仅演示了CHANNEL_开头的几个常用事件
                    if (eventName.startsWith("CHANNEL_")) {
                        String calleeNumber = event.getEventHeaders().get("Caller-Callee-ID-Number");
                        String callerNumber = event.getEventHeaders().get("Caller-Caller-ID-Number");
                        switch (eventName) {
                            case "CHANNEL_CREATE":
                                System.out.println("CHANNEL_CREATE,zhujiao:" + callerNumber + ",beijiao:" + calleeNumber);
                                break;
                            case "CHANNEL_BRIDGE":
                                System.out.println("CHANNEL_BRIDGE,zhujiao:" + callerNumber + ",beijiao:" + calleeNumber);
                                break;
                            case "CHANNEL_ANSWER":
                                System.out.println("CHANNEL_ANSWER,zhujiao:" + callerNumber + ",beijiao:" + calleeNumber);
                                break;
                            case "CHANNEL_HANGUP":
                                String response = event.getEventHeaders().get("variable_current_application_response");
                                String hangupCause = event.getEventHeaders().get("Hangup-Cause");
                                System.out.println("CHANNEL_HANGUP,zhujiao:" + callerNumber + ",beijiao:" + calleeNumber + " , response:" + response + " ,hangup cause:" + hangupCause);
                                break;
                            default:
                                break;
                        }
                    }
                }
                @Override
                public void backgroundJobResultReceived(EslEvent event) {
                    String jobUuid = event.getEventHeaders().get("Job-UUID");
                    System.out.println("async callback:" + jobUuid);
                }
            });
            client.setEventSubscriptions("plain", "all");
            //这里必须检查，防止网络抖动时，连接断开
            if (client.canSend()) {
                System.out.println("connect suc,begin to call...");
                //（异步）向1000用户发起呼叫，用户接通后，播放音乐/tmp/demo1.wav
//                String callResult = client.sendAsyncApiCommand("originate", "{origination_caller_id_number=02985703161,origination_caller_id_name=02985703161}sofia/external/13605178993@211.157.158.106:5060 &echo");
                String callResult = client.sendAsyncApiCommand("originate", "user/1013 &echo");
                System.out.println("api uuid:" + callResult);
            }
        } catch (InboundConnectionFailure inboundConnectionFailure) {
            System.out.println("连接失败！");
            inboundConnectionFailure.printStackTrace();
        }
    }
}
